<template>
  <div class="login-page">
    <div class="logo">
      <van-image
        width="60"
        height="60"
        src="/src/assets/images/logo.png"
      />
      <div class="title">村宝易购</div>
    </div>

    <div class="login-form">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="username"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          >
            <template #label>
              <van-icon name="user-o" style="margin-right: 5px;" />
              用户名
            </template>
          </van-field>

          <van-field
            v-model="password"
            type="password"
            name="password"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          >
            <template #label>
              <van-icon name="lock" style="margin-right: 5px;" />
              密码
            </template>
          </van-field>
        </van-cell-group>

        <div class="form-footer">
          <div class="remember-pwd">
            <van-checkbox v-model="rememberPassword">记住密码</van-checkbox>
          </div>
          <div class="forgot-pwd" @click="onForgotPassword">忘记密码？</div>
        </div>

        <div style="margin: 24px 16px">
          <van-button round block type="primary" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
    </div>

    <div class="other-login">
      <div class="divider">
        <span class="line"></span>
        <span class="text">其他登录方式</span>
        <span class="line"></span>
      </div>
      <div class="login-methods">
        <div class="method" @click="onWechatLogin">
          <van-icon name="wechat" color="#07c160" size="24" />
          <span>微信登录</span>
        </div>
        <div class="method" @click="onPhoneLogin">
          <van-icon name="phone-o" color="#1989fa" size="24" />
          <span>手机号登录</span>
        </div>
      </div>
    </div>

    <div class="register-link">
      还没有账号？
      <router-link to="/register" class="link">立即注册</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { useUserStore } from '@/store/modules/user'

const router = useRouter()
const userStore = useUserStore()

const username = ref('')
const password = ref('')
const rememberPassword = ref(false)

// 表单提交
const onSubmit = async (values) => {
  try {
    const success = await userStore.login(values.username, values.password)
    console.log(success);
    
    if (success) {
      showToast('登录成功')
      // 根据是否是管理员跳转到不同页面
      if (userStore.getIsAdmin) {
        router.replace('/admin/home')
      } else {
        router.replace('/home')
      }
    }
  } catch (error) {
    showToast('登录失败，请稍后重试')
  }
}

// 忘记密码
const onForgotPassword = () => {
  showToast('暂未开放，请联系管理员')
}

// 微信登录
const onWechatLogin = () => {
  showToast('暂未开放，请使用账号密码登录')
}

// 手机号登录
const onPhoneLogin = () => {
  router.push('/phone-login')
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  padding: 40px 16px;
  background-color: #fff;

  .logo {
    text-align: center;
    margin-bottom: 40px;

    .title {
      margin-top: 12px;
      font-size: 24px;
      font-weight: 600;
      color: #323233;
    }
  }

  .login-form {
    .form-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 16px;
      font-size: 14px;

      .forgot-pwd {
        color: #1989fa;
      }
    }
  }

  .other-login {
    margin-top: 40px;

    .divider {
      display: flex;
      align-items: center;
      color: #969799;
      font-size: 14px;
      
      .line {
        flex: 1;
        height: 1px;
        background-color: #ebedf0;
      }
      
      .text {
        padding: 0 16px;
      }
    }

    .login-methods {
      display: flex;
      justify-content: center;
      margin-top: 24px;
      
      .method {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin: 0 24px;
        font-size: 12px;
        color: #323233;
        
        span {
          margin-top: 8px;
        }
      }
    }
  }

  .register-link {
    margin-top: 24px;
    text-align: center;
    font-size: 14px;
    color: #969799;

    .link {
      color: #1989fa;
      margin-left: 4px;
    }
  }
}
</style> 